package com.qiantu.phone.ui.activity;

//kgx壳子页面
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.hjq.base.BaseAdapter;
import com.hjq.base.BaseDialog;
import com.hjq.http.listener.HttpCallback;
import com.qiantu.api.db.LLControllerDBManager;
import com.qiantu.api.db.LLDeviceDBManager;
import com.qiantu.api.db.LLRoomDBManager;
import com.qiantu.api.entity.ControllerBean;
import com.qiantu.api.entity.DeviceBean;
import com.qiantu.api.entity.KGXAddData;
import com.qiantu.api.entity.KGXcollectionData;
import com.qiantu.api.http.LLHttpManager;
import com.qiantu.api.http.model.HttpData;
import com.qiantu.phone.R;
import com.qiantu.phone.aop.Log;
import com.qiantu.phone.app.AppActivity;
import com.qiantu.phone.app.AppApplication;
import com.qiantu.phone.event.RefreshDeviceListEvent;
import com.qiantu.phone.event.RefreshFloorRoomEvent;
import com.qiantu.phone.glide.GlideApp;
import com.qiantu.phone.ui.adapter.KGXAddAdapter;
import com.qiantu.phone.ui.adapter.LoadDeviceAdapter;
import com.qiantu.phone.ui.adapter.TLPKGXAdapter;
import com.qiantu.phone.ui.dialog.ConfirmCancelHintDialog;
import com.qiantu.phone.ui.uiutil.LLDeviceUtil;
import com.qiantu.phone.widget.StatusLayout;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Call;

public class ControllerTLPKGXActivity extends AppActivity {
    private String controllerSerialNo;
    private ImageView iconController;
    private TextView tvControllerName;
    private TextView tvControllerId;
    private TextView tvControllerStatus;
    private View btnSetting;
    private TLPKGXAdapter tlpkgxAdapter;
    private StatusLayout statusLayout;
    private RecyclerView loadDeviceRecyclerView;
    private LinearLayout emptyLayout;
    private TextView btn;
    private List<ControllerBean> controllerBeanList;
    private ControllerBean controllerBean;
    private List<KGXcollectionData> addData = new ArrayList<>();

    private SmartRefreshLayout smartRefreshLayout;

    @Log
    public static void start(Context context, String controllerSerialNo) {
        Intent intent = new Intent(context, ControllerTLPKGXActivity.class);
        intent.putExtra("controllerSerialNo", controllerSerialNo);
        if (!(context instanceof Activity)) {
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }
        context.startActivity(intent);
    }
    @Override
    protected int getLayoutId(){
        return R.layout.activity_controller_tlpkgx;
    }
    @Override
    protected void onResume() {
        super.onResume();
        getdevicecollectlist();
    }
    @Override
    protected void initView(){
        controllerSerialNo = getString("controllerSerialNo");
        iconController = findViewById(R.id.icon_controller);
        tvControllerName = findViewById(R.id.tv_controller_name);
        tvControllerId = findViewById(R.id.tv_controller_id);
        tvControllerStatus = findViewById(R.id.tv_controller_status);
        btnSetting = findViewById(R.id.btn_setting);
        statusLayout = findViewById(R.id.status_layout);
        loadDeviceRecyclerView = findViewById(R.id.load_device_recycler_view);
        emptyLayout = findViewById(R.id.ll_Noequipment);
        btn = findViewById(R.id.btn);
        smartRefreshLayout = findViewById(R.id.smart_refresh_layout);
        smartRefreshLayout.setEnableRefresh(false);
        smartRefreshLayout.setEnableLoadMore(false);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(), 3);
        loadDeviceRecyclerView.setLayoutManager(gridLayoutManager);
        tlpkgxAdapter = new TLPKGXAdapter(this);
        tlpkgxAdapter.setOnItemClickListener(new BaseAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(RecyclerView recyclerView, View itemView, int position) {
                KGXcollectionData item = (KGXcollectionData)itemView.getTag();
                if (item.getControllerSerialNo() == null){
                    KGXAddActivity.start(getContext(), controllerSerialNo,addData.size()-1);
                }else {
                    if (item.getType() == 22){//弱电旋钮空调面板
                        KGXControllerSettingActivity.start(getContext(), item,addData);
                    }else {
                        KGXControllerDeviceAcitvity.start(getContext(), item,addData);
                    }
                }
            }
        });
        tlpkgxAdapter.setOnChildClickListener(R.id.btn_device_operate, new BaseAdapter.OnChildClickListener() {
            @Override
            public void onChildClick(RecyclerView recyclerView, View childView, int position) {

            }
        });
        loadDeviceRecyclerView.setAdapter(tlpkgxAdapter);
        btnSetting.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ControllerSettingActivity.start(getContext(), controllerSerialNo);
            }
        });
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showDeleteHintDialog();
            }
        });
    }

    @Override
    protected void initData(){
        controllerBean = LLControllerDBManager.getInstance(getContext()).getControllerBySerialNo(controllerSerialNo);

    }

    private void updateView(){
        if(addData.isEmpty()){
            emptyLayout.setVisibility(View.VISIBLE);
            statusLayout.setVisibility(View.GONE);
        }else{
            emptyLayout.setVisibility(View.GONE);
            statusLayout.setVisibility(View.VISIBLE);
        }

        setTitle(controllerBean.getName());
        GlideApp.with(iconController).load(controllerBean.getImageUrl()).into(iconController);
        tvControllerName.setText(controllerBean.getName());
        tvControllerId.setText(controllerBean.getCodeNo());
        if(controllerBean.isIsDisable()){
            tvControllerStatus.setText(R.string.stop_using);
            tvControllerStatus.setEnabled(false);
            tvControllerStatus.setSelected(true);
        }else{
            switch (controllerBean.getStateType()){
                case 0:
                    tvControllerStatus.setText(R.string.offline);
                    tvControllerStatus.setEnabled(false);
                    tvControllerStatus.setSelected(false);
                    break;
                case 1:
                    tvControllerStatus.setText(R.string.online);
                    tvControllerStatus.setEnabled(true);
                    tvControllerStatus.setSelected(false);
                    break;
                case 2:
                    tvControllerStatus.setText(R.string.fault);
                    tvControllerStatus.setEnabled(false);
                    tvControllerStatus.setSelected(false);
                    break;
            }
        }
    }

    private void getdevicecollectlist(){
        LLHttpManager.getdevicecollectlist(this, controllerSerialNo, new HttpCallback<HttpData<List<KGXcollectionData>>>(this){
            @Override
            public void onStart(Call call) {
                super.onStart(call);
            }

            @Override
            public void onSucceed(HttpData<List<KGXcollectionData>> result) {
                super.onSucceed(result);
                if (result.getCode() == 10000){
                    addData = result.getData();
                    if (!addData.isEmpty()){
                        addData.add(new KGXcollectionData());
                        tlpkgxAdapter.setData(addData);
                    }
                    updateView();

                } else {
                    showDialog(result.getMessage());
                    finish();
                }

            }

            @Override
            public void onFail(Exception e) {
                super.onFail(e);
            }

            @Override
            public void onEnd(Call call) {
                super.onEnd(call);
            }
        });
    }

    private void gotoAddView(){
        if (!addData.isEmpty()){
            KGXAddActivity.start(getContext(), controllerSerialNo,addData.size()-1);
        }else {
            KGXAddActivity.start(getContext(), controllerSerialNo,0);
        }

    }

    private BaseDialog mConfirmCancelHintDialog;
    private ConfirmCancelHintDialog.Builder mConfirmCancelHintDialogBuilder;
    private void showDeleteHintDialog() {
        if (mConfirmCancelHintDialog ==  null) {
            mConfirmCancelHintDialogBuilder = new ConfirmCancelHintDialog.Builder(getActivity());
            mConfirmCancelHintDialog = mConfirmCancelHintDialogBuilder.create();
        }
        mConfirmCancelHintDialogBuilder.setListener(new ConfirmCancelHintDialog.OnListener() {
            @Override
            public void onLeftClick(BaseDialog dialog) {
                mConfirmCancelHintDialogBuilder.dismiss();
            }

            @Override
            public void onRightClick(BaseDialog dialog) {
                mConfirmCancelHintDialogBuilder.dismiss();
                gotoAddView();
            }
        });
        mConfirmCancelHintDialogBuilder.setRightText(getString(R.string.ok));
        mConfirmCancelHintDialogBuilder.setContent("该处为手动添加面板类型，列表顺序即是对应的真实面板的485地址；比如负载界面第3个为温控面板，那么就需要把墙上的某个温控面板485地址设置为“3”！");
        mConfirmCancelHintDialog.show();
    }
}
